package edu.spec.sys.controller;

import edu.spec.sys.model.Command;
import edu.spec.sys.model.Module;
import edu.spec.sys.model.dto.ExtGridReturn;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.Status;
import edu.spec.sys.service.iface.ICommandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/command")
public class CommandController {

    @Inject
    private ICommandService commandService;

    /**
     * 管理RoleCommand
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/listCommandByRole/{roleId}")
    public Object listCommandByRole(@PathVariable int roleId){
        List<Command> roleCommand = commandService.listCommandByRole(roleId);
        MvcReturn er = new MvcReturn(true, roleCommand);
        return er;
    }

	@ResponseBody
	@RequestMapping("/listCommandByModule")
	public Object listCommandByModule(@RequestParam int id){
		List<Command> cs = commandService.listCommandByModule(id);
		return new ExtGridReturn((long)cs.size(), cs);
	}

	@ResponseBody
	@RequestMapping("/delete/{id}")
	public Object delete(@PathVariable int id){
		commandService.delete(id);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Object add(Command c, @RequestParam int mId){
		c.setCreateTime(new Date());
		Module m = new Module();
		m.setId(mId);
		c.setModule(m);
		commandService.add(c);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public Object update(Command c){
		Command dbc = commandService.load(c.getId());
		dbc.setStatus(c.getStatus());
		dbc.setDes(c.getDes());
		dbc.setChinese(c.getChinese());
		dbc.setIconClz(c.getIconClz());
		dbc.setOrderNum(c.getOrderNum());
		commandService.update(dbc);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/toggleStatus/{id}")
	public Object toggleStatus(@PathVariable int id){
		Command c = commandService.load(id);
		if(c.getStatus()== Status.DISABLED){
			c.setStatus(Status.ENABLED);
		}else{
			c.setStatus(Status.DISABLED);
		}
		commandService.update(c);
		return new MvcReturn(true);
	}
}
